package com.example.day8_sp;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    SharedPreferences preferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态授权
        requestPermissions(new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.WRITE_CONTACTS,
                Manifest.permission.READ_SMS,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_CALL_LOG,
                Manifest.permission.READ_CALL_LOG
        },100);
        preferences = getSharedPreferences("2208A", MODE_PRIVATE);
    }

    public void insert(View view) {
        /*//获取SP对象
        SharedPreferences preferences = getSharedPreferences("2208A", MODE_PRIVATE);*/
        //获取Editor对象
        SharedPreferences.Editor edit = preferences.edit();
        //添加数据
        edit.putString("name","韩家树");
        edit.putInt("age",18);
        edit.putLong("height",183l);
        edit.putBoolean("isBoy",true);
        edit.putFloat("weight",70.5f);
        edit.putString("name","邓孟卓");
        edit.remove("age");//删除指定
        edit.commit();

    }

    public void query(View view) {
        String name = preferences.getString("name", "张三");
        int age = preferences.getInt("age", 0);
        Toast.makeText(this, "获取到的名字为"+name+"年龄是"+age, Toast.LENGTH_SHORT).show();
    }

    public void clear(View view) {
        preferences.edit().clear().commit();
    }
}